package cycling.taxi.com.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.LruCache;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

public class FlashActivity extends AppCompatActivity {
    private ImageView anim_img;
    // 动画
    private Animation anim;
    private Animation anim_out;
    private List<Bitmap> bitmaps = null;
    RequestQueue requestQueue;
    int index=0;
    String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/cycling.taxi.com";

    // RequestQueue queue= Volley.newRequestQueue(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash);
        ExitApplication.getInstance().addActivity(this);
        bitmaps = getPictures(dir);
        anim_img = (ImageView) findViewById(R.id.anim_img);
        anim = AnimationUtils.loadAnimation(this, R.anim.init_alp);
        anim_out = AnimationUtils.loadAnimation(this, R.anim.out_alp);
        if(bitmaps.size()==0){
            anim_img.startAnimation(anim);
            initViewOper();
        }else {
            anim_img.setImageBitmap(bitmaps.get(index));
            index++;
            anim_img.startAnimation(anim);
            initViewOper();
        }
        //
        //  开始动画

        //  for(int i=0;i<bitmaps.size();i++){

        //  }

        //    getImg();
    }

    public void initViewOper() {
        // 动画结束 跳转
        anim.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            // 动画结束
            @Override
            public void onAnimationEnd(Animation animation) {
                // 动画结束之后 跳转到
                if (index == bitmaps.size()) {
                    String phone = SPUtil.getInstance().getData("beanJson");
                    String password = SPUtil.getInstance().getData("password");
                    Intent intent = null;
                    if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)) {
                        intent = new Intent(FlashActivity.this, HomeAty.class);
                    } else {
                        intent = new Intent(FlashActivity.this, LoginAty.class);
                    }
                    startActivity(intent);
                    finish();
                }
                anim_img.startAnimation(anim_out);
                //  anim_img.setImageBitmap(bitmaps.get(1));
                //  anim_img.startAnimation(anim);
//                String phone= SPUtil.getInstance().getData("beanJson");
//                String password=SPUtil.getInstance().getData("password");
//                Intent intent = null;
//                if (!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(password)) {
//                    intent = new Intent(FlashActivity.this, HomeAty.class);
//                } else {
//                    intent = new Intent(FlashActivity.this, LoginAty.class);
//                }
//                startActivity(intent);
//                finish();
            }
        });
        anim_out.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                anim_img.setImageBitmap(bitmaps.get(index));
                anim_img.startAnimation(anim);
                index++;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    public List<Bitmap> getPictures(final String strPath) {
        List<String> list = new ArrayList<String>();
        List<Bitmap> bitmapList = new ArrayList<Bitmap>();
        File file = new File(strPath);
        File[] allfiles = file.listFiles();
        if (allfiles == null) {
            return null;
        }
        for (int k = 0; k < allfiles.length; k++) {
            final File fi = allfiles[k];
            if (fi.isFile()) {
                int idx = fi.getPath().lastIndexOf(".");
                if (idx <= 0) {
                    continue;
                }
                String suffix = fi.getPath().substring(idx);
                if (suffix.toLowerCase().equals(".jpg") ||
                        suffix.toLowerCase().equals(".jpeg") ||
                        suffix.toLowerCase().equals(".bmp") ||
                        suffix.toLowerCase().equals(".png") ||
                        suffix.toLowerCase().equals(".gif")) {
                    list.add(fi.getPath());
                    Bitmap bm = BitmapFactory.decodeFile(fi.getPath());
                    bitmapList.add(bm);
                }
            }
        }
        return bitmapList;
    }
}
